<?php
$host = '127.0.0.1';
$port = 3128;
$password = 'password';
$password = 'admin_password';
$userid = 'rendonguid';
$username = 'rendongname';

$touserid = 'userid_5';

ini_set("memory_limit", "1024M");



$fp = fsockopen($host, $port, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
	$message = s(100) . str($userid) . str($username) . str($password);
	$out = i(strlen($message)) . $message;

    fwrite($fp, $out);

	$len = current(unpack("i", fread($fp, 4)));
	echo 'len:' . $len;
	$data = unpack("stype/istatus", fread($fp, $len));
	echo "return type:{$data['type']}, status:{$data['status']}\n";


	// 发送管理消息
/**
	$content = "admin message";
	$type = 'type';
	$message = s(105) . str($type) . str($content);

	$out = i(strlen($message)) . $message;
	fwrite($fp, $out);
/**/

	// 查看服务器状态信息
//**
	$message = s(500);

	$out = i(strlen($message)) . $message;
	fwrite($fp, $out);

	$len = current(unpack("i", fread($fp, 4)));
	$data = unpack("stype/imessage_len", fread($fp, 6));
	echo 'type:' . $data['type'] . " message_len:" . $data['message_len'] . "\n";
	$str_msg = current(unpack("a" . $data['message_len'], fread($fp, $data['message_len'])));
	echo $str_msg;
/**/

    fclose($fp);
}

// 封装字符串
function str($str)
{
	return pack("i",strlen($str)) .   $str;
}

// 封装int
function i($i)
{
	return pack("i", $i);
}

// 封装short
function s($s)
{
	return pack("s", $s);
}
?>
